<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Instructor extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
		session_start();
    }
	
	public function index()
	{	
		if(($this->is_logged_in())){
			$username = $_SESSION['username'];
			$this->load->helper(array('url','form'));
			
			$this->load->model("instructor_model");

			$username = $_SESSION['username'];
			$result = $this->instructor_model->getInstData($username);
			
			foreach($result as $item){
				$data[] = $item;
			}
			/* add employee number to sessions. */
			$_SESSION['emp_id'] = $data[1];
		
			$data['title'] = "Instructor's profile";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/instructor_homepage', $result);
			$this->load->view('templates/footer');	
		}else{
			redirect('login');
		}
	}
	
	public function loadBasicInfo(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model('instructor_model');
			
			$emp_id = $_SESSION['emp_id'];
			$result = $this->instructor_model->getInstData2($emp_id);
			
			$result[$emp_id] = $emp_id;
		
			$data['title'] = "Instructor's Basic Information";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/instructor_modifybasicinfo',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function modify_instructor_basicinfo(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
		
			$emp_id = $_SESSION['emp_id'];
			$cpno = $this->input->post('mobile_no');
			$designation = $this->input->post('designation');
			$rank = $this->input->post('rank');
			$facultyroom = $this->input->post('facultyroom');
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			$this->form_validation->set_rules('mobile_no','Mobile Number','trim|integer|min_length[11]|max_length[11]');
			$this->form_validation->set_rules('designation','Designation','trim|alpha|min_length[5]|max_length[25]');
			$this->form_validation->set_rules('rank','Rank','trim|alpha|min_length[5]|max_length[25]');
			$this->form_validation->set_rules('facultyroom','Faculty Room','trim|min_length[4]|max_length[5]');
			
			if($this->form_validation->run() == FALSE){
				$this->load->model('instructor_model');
				$result = $this->instructor_model->getInstData2($emp_id);
			
				foreach($result as $item){
					$data[] = $item;
				}
				
				$result[$emp_id] = $emp_id;
			
				$data['title'] = "Instructor's Basic Information";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/instructor_modifybasicinfo',$result);
				$this->load->view('templates/footer');
			}else{
				$data = array(
					'emp_id' => $emp_id,
					'cpno' => $cpno,
					'designation' => $designation,
					'rank' => $rank,
					'facultyroom' =>$facultyroom,
					);
				$this->load->model('instructor_model');
				$this->instructor_model->modifybasicinfo($data);
				
				$uname = $_SESSION['username'];
				$message = $uname . " modified his/her Basic Information.";
				$this->instructor_model->addToLog($message,$uname);
				
				redirect('/instructor/view/');
			}
		}else{
			redirect('login');
		}
	}
	
	public function loadChangePassword(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$emp_id = $_SESSION['emp_id'];
			
			$data['title'] = "Change Password";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/instructor_changepassword');
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function changePassword(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$emp_id = $_SESSION['emp_id'];
			
			$pw1 = $this->input->post('password');
			$pw2 = $this->input->post('cpassword');
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]|max_length[20]|matches[cpassword]');
			$this->form_validation->set_rules('cpassword', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
		
			if($this->form_validation->run() == FALSE){
				$data['title'] = "Change Password";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/instructor_changepassword');
				$this->load->view('templates/footer');
			}else{
				$data = array(
							'pw' => md5($pw1),
							'emp_id' => $emp_id,
						);
				$this->load->model('instructor_model');
				$this->instructor_model->changePW($data);
				
				$uname = $_SESSION['username'];
				$message = $uname . " changed his/her password.";
				$this->instructor_model->addToLog($message,$uname);
				
				redirect('instructor/view');
			}
		}else{
			redirect('login');
		}
	}
	
	public function view(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model("instructor_model");
			
			$username = $_SESSION['username'];
			$result = $this->instructor_model->getInstData($username);
			
			foreach($result as $item){
				$data[] = $item;
			}
			
			$data['title'] = 'Instructor';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/instructor_homepage',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function logout(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$_SESSION['login'] = 0;
			session_destroy();
			redirect('login');
		}else{
			redirect('login');
		}
	}	
	
	public function instructor_student_info()
	{
		if(($this->is_logged_in())){
			$this->load->helper('form');
			$data['title'] = substr(implode(" ", $this->input->post('show_student_info')), 0, strlen(implode($this->input->post('show_student_info')))-19 );	
			$this->load->view('templates/header', $data);
			$this->load->view('pages/instructor_student_info');
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function search_student()
	{
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->library('form_validation');
			
			$student = $this->input->post('student');
			
			$this->form_validation->set_rules('student', 'student', 'trim|required');
			
			if($this->form_validation->run() == FALSE)
			{
				$username = $_SESSION['username'];
				
				$this->load->helper(array('url','form'));
				
				$this->load->model('instructor_model');
				$this->instructor_model->get_inst_data($username);
				$data['title'] = 'Instructor';
				$info['info'] =  $this->instructor_model->get_inst_data($username);
				$this->load->view('templates/header', $data);
				$this->load->view('pages/instructor_homepage', $info);
				$this->load->view('templates/footer');
			}
			else
			{
				$input_string = $this->input->post('student');
				$data['title'] = "Search results for '$input_string'";
				$option = $this->input->post('search');
				
				$this->load->model('instructor_model');
				
				if($this->input->post('search') == 'name')
				{
					$search_results['result'][] = $this->instructor_model->search_by_name($input_string);
				}
				else
				{
					$search_results['result'][] = $this->instructor_model->search_by_student_number($input_string);
				}
				$this->load->view('templates/header', $data);
				$this->load->view('pages/instructor_search_student', $search_results);
				$this->load->view('templates/footer');	
			}
		}else{
			redirect('login');
		}
	}
	
	public function course_exists($course_input)
	{
		$this->load->library('form_validation');
		$this->load->model('instructor_model');
		$this->load->helper(array('url','form'));
		if($this->instructor_model->course_exists($course_input) == FALSE)
		{
			$this->form_validation->set_message('course_exists', 'The course you entered does not exist');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	public function view_qualified_students()
	{
		if(($this->is_logged_in())){
			$this->load->helper(array('url', 'form'));
			$this->load->library('form_validation');
			
			$course_input = strtoupper($this->input->post('course'));
			
			$this->form_validation->set_rules('course', 'course', 'trim|required|callback_course_exists');
			
			$this->load->model('instructor_model');
			$this->load->helper(array('url','form'));
			
			if($this->form_validation->run()== FALSE)
			{
				$username = $_SESSION['username'];		
				$this->instructor_model->get_inst_data($username);
				$data['title'] = 'Instructor';
				$info['info'] =  $this->instructor_model->get_inst_data($username);
				$this->load->view('templates/header', $data);
				$this->load->view('pages/instructor_homepage', $info);
				$this->load->view('templates/footer');
			}
			else
			{
				$data['title'] = "Qualifiers for $course_input";
				
				$result = $this->instructor_model->get_qualified_students($course_input);
				if($result[0] == NULL)
				{
					echo "The selected course has no prerequisite/s";
				}
				else
				{
					for($i=0;$i<sizeof($result);$i++)
					{
						echo implode(" ", $result[$i]).'<br />';
					}
				}
				$this->load->view('templates/header', $data);
				$this->load->view('pages/instructor_course_qualifiers');
				$this->load->view('templates/footer');	
			}
		}else{
			redirect('login');
		}
	}
	
	public function view_students_associated()
	{
		$this->load->helper('url');
		
		$username = $_SESSION['username'];
		$data['title'] = "Students associated with $username";
		$data['links'] = 1;
			
		$this->load->view('templates/header', $data);
		$this->load->view('pages/instructor_associated_students');
		$this->load->view('templates/footer');
	}
	
	public function view_current_advisees()
	{
		$this->load->helper('url');
		$this->load->model('instructor_model');
		
		$username = $_SESSION['username'];
		$data['title'] = "Current advisees of $username";
		$data['links'] = 2;
		
		$result = $this->instructor_model->get_emp_id($username);
		$emp_id = implode($result[0]);
		
		$result2 = $this->instructor_model->get_name($username);
		$name = implode($result2[0]);
		
		$advisees = $this->instructor_model->get_current_sp_advisees($emp_id);
		
		echo 'Current SP Advisees'.'<br />';
		for($i=0;$i<sizeof($advisees);$i++)
			{
				echo implode(" ", $advisees[$i]).'<br />';
			}
		
		$advisees = $this->instructor_model->get_registration_advisees($name);
		
		echo 'Registration Advisees'.'<br />';
		for($i=0;$i<sizeof($advisees);$i++)
			{
				echo implode(" ", $advisees[$i]).'<br />';
			}
		
		$this->load->view('templates/header', $data);
		$this->load->view('pages/instructor_associated_students');
		$this->load->view('templates/footer');	
	}
	
	public function view_previous_advisees()
	{
		$this->load->helper('url');
		$this->load->model('instructor_model');
		
		$username = $_SESSION['username'];
		$data['title'] = "Previous advisees of $username";
		$data['links'] = 3;
		
		$result = $this->instructor_model->get_emp_id($username);
		$emp_id = implode($result[0]);
		
		$advisees = $this->instructor_model->get_previous_sp_advisees($emp_id);
		
		echo 'Previous SP Advisees'.'<br />';
		for($i=0;$i<sizeof($advisees);$i++)
			{
				echo substr(implode(" ", $advisees[$i]), 0, strlen(implode(" ", $advisees[$i]))-10);
				$inst_name = $this->instructor_model->get_inst_name(substr(implode(" ", $advisees[$i]), -10));
				echo 'transferred to '.implode($inst_name[0]).'<br />';
			}
			 
		$this->load->view('templates/header', $data);
		$this->load->view('pages/instructor_associated_students');
		$this->load->view('templates/footer');	
	}
}